package com.glut.gultguy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.glut.gultguy.entity.Address;
import com.glut.gultguy.entity.User;
import com.glut.gultguy.mapper.AddressMapper;
import com.glut.gultguy.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glut.gultguy.service.UserService;
import com.glut.gultguy.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-09-05
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Autowired(required = false)
    private AddressMapper mapper;

    @Autowired
    UserService userService;
    @Autowired(required = false)
    private SnowflakeIdWorker idWorker;
    @Override
    public List<Address> getAddr(String publisherId) {
        User user = userService.getById(publisherId);
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("_openid",user.get_openid());
        return mapper.selectList(queryWrapper);
    }

    @Override
    public Map<String,Integer> addAddr(Address address) {
        address.setId(idWorker.nextId()+"");
        int t = mapper.insert(address);
        Map<String,Integer> map = new HashMap<>();
        if (t==1){
            map.put("code",200);
        }else {
            map.put("code",500);
        }
        return map;
    }

    @Override
    public List<Address> getAddrByOpenid(String openid) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("_openid",openid);
        return mapper.selectList(queryWrapper);
    }
}
